// Game Client XML protocol object model.
//
namespace("Game.Protocol");

// Game.Request object
//TODO: make this bindable with response
Game.Request = function(data, onLoad, onError, params) {
    this.data    = params ? data.create(params) : data;
    this.headers = data.headers;
    this.onLoad  = onLoad;
    this.onError = onError;
}

Game.Request.prototype = {
    
    send: function() {
        var request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        request += this.getRequestXml();
        var loader = new Net.ContentLoader({
            url:     this.data.handler,
            params:  request,
            onload:  this.onLoad,
            onerror: this.onError,
            headers: this.headers,
            caller:  this,
            contentType : Game.Protocol.REQUEST_TYPE
        });
    },
    
    getRequestXml: function() {
        return Sarissa.xmlize(this.data.request, Game.Protocol.Game_REQUEST_NODE);
    }//,
    
    //    onLoad: function(loader)
    //    {
    //        alert("!");
    //    }
}

Game.Response = function(responseXml) {
    if (responseXml && responseXml.documentElement)
        loadObjectFromXmlNode(this, responseXml.documentElement);
}

Game.HTMLResponse = function(target) {
   // this.target = target;
}

Game.HTMLResponse.prototype = {
    load : function(jseval) {
        eval(jseval);
    },
    
    call : function(a,b) {
        alert("call");
    }    
}

Game.FormRequest = function(data,onLoad,onError,params) {
    this.headers = data.headers;
    this.onLoad  = onLoad;
    this.onError = onError;
    this.data    = params ? data.create(params) : data;
}

Game.FormRequest.prototype = {
    
    send : function() {
        var request = "";
        $H(this.data.request).each( function(pair) {
            request +="&"+ pair.key+"="+pair.value;                                
        });
        request = request.substring(1);
        var loader = new Net.ContentLoader({
            url:     this.data.handler,
            params:  request,
            onload:  this.onLoad,
            onerror: this.onError,
            headers: this.headers,
            caller:  this,
            contentType : Game.Protocol.FORM_TYPE
        });
    },
}

// Game.Response object
Game.Response = function(responseXml) {
    if (responseXml && responseXml.documentElement)
        loadObjectFromXmlNode(this, responseXml.documentElement);
}

Game.Response.prototype = {
    load : function(onLoad) {        
        if (responseXml && responseXml.documentElement)
            loadObjectFromXmlNode(this, responseXml.documentElement);
    }
}

// Protocol implementation
Game.Protocol.Game_REQUEST_NODE = "GameRequest";

Game.Protocol.SSID_HEADER = "SSID"; // Server Session ID
Game.Protocol.CSID_HEADER = "CSID"; // Client Session ID (in Cookies)
Game.Protocol.TYPE_HEADER = "Content-Type";

Game.Protocol.REQUEST_TYPE = "text/xml;charset=utf-8";
Game.Protocol.FORM_TYPE = "application/x-www-form-urlencoded;charset=utf-8";


// Each request objest should contain following properties:
// handler: url of request handler
// request: request object wich properties hierarchy will be transparently serialized to XML

/**
 *  Basic class for requests.
 */
Game.Request.Base = function() {
    //+Do not modify it
    this.request = {
        Command : {ID : ""}
    };
    this.headers = {};
    //-Do not modify it
}

Game.FormRequest.Base = function() {
    //+Do not modify it
    this.request = {        
    };
    this.headers = {};
    //-Do not modify it
}